package com.ibeetl.admin.aod.service.excel.parser;

import com.ibeetl.admin.aod.entity.AodNumberAttribution;
import com.ibeetl.admin.aod.util.DateUtils;
import com.ibeetl.admin.aod.web.dto.AodIndustryDataDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class FarFileParser extends AbstractFileParser {

    @Override
    public AodIndustryDataDto parse(String line, Map<String,AodNumberAttribution> aodNumberAttributionMap) {

        AodIndustryDataDto aodIndustryDataDto;
        try {
            String[] lines = line.split(",");
            aodIndustryDataDto = new AodIndustryDataDto();
            aodIndustryDataDto.setAccount(lines[1]);
            aodIndustryDataDto.setPhoneNumber(lines[3]);
            String pNum = lines[3].substring(0,7);
            if (aodNumberAttributionMap.containsKey(pNum)) {
                aodIndustryDataDto.setNumberAttribution(aodNumberAttributionMap.get(pNum).getId());
                aodIndustryDataDto.setAodNumberAttribution(aodNumberAttributionMap.get(pNum));
            }
            aodIndustryDataDto.setSendTime(DateUtils.parse(lines[10],"yyyy/MM/dd HH:mm:ss"));
            aodIndustryDataDto.setStatus(lines[9]);
            String sendContent = lines[13];
            aodIndustryDataDto.setSendContent(sendContent);
            if (StringUtils.isNotEmpty(sendContent) && sendContent.contains(FileParser.LABEL_START_STR) && sendContent.contains(FileParser.LABEL_END_STR)) {
                aodIndustryDataDto.setLabel(sendContent.substring(sendContent.indexOf(FileParser.LABEL_START_STR) + 1, sendContent.indexOf(FileParser.LABEL_END_STR)));
            }

        } catch (Exception e) {
            log.error("数据格式错误" + line, e);
            aodIndustryDataDto = null;
        }

        return aodIndustryDataDto;

    }
}
